/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.config.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.common.utils.BaseUtil;
import com.icinfo.cloud.provider.common.utils.CustomCheckParamUtil;
import com.icinfo.cloud.provider.punish.config.dto.ConfigElectricArchiveCatalogDetailSaveDto;
import com.icinfo.cloud.provider.punish.config.dto.ConfigElectricArchiveCatalogSaveDto;
import com.icinfo.cloud.provider.punish.config.mapper.ConfigElectricArchiveCatalogDetailMapper;
import com.icinfo.cloud.provider.punish.config.mapper.ConfigElectricArchiveMainMapper;
import com.icinfo.cloud.provider.punish.config.model.ConfigElectricArchiveCatalogDetail;
import com.icinfo.cloud.provider.punish.config.model.ConfigElectricArchiveMain;
import com.icinfo.cloud.provider.punish.config.service.IConfigElectricArchiveCatalogDetailService;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 描述: 电子卷宗配置目录信息 config_electric_archive_catalog_detail 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年12月08日
 */
@Service
public class ConfigElectricArchiveCatalogDetailServiceImpl extends MyBatisServiceSupport implements IConfigElectricArchiveCatalogDetailService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(ConfigElectricArchiveCatalogDetailServiceImpl.class);

    @Autowired
    private ConfigElectricArchiveCatalogDetailMapper configElectricArchiveCatalogDetailMapper;
    @Autowired
    private ConfigElectricArchiveMainMapper configElectricArchiveMainMapper;

    @Override
    public List<ConfigElectricArchiveCatalogDetail> getElectricArchiveCatalog(String archiveId) {
        Example example =new Example(ConfigElectricArchiveCatalogDetail.class);
        example.createCriteria().andEqualTo("archiveId",archiveId);
        return configElectricArchiveCatalogDetailMapper.selectByExample(example);
    }

    @Override
    public int saveElectricArchiveCatalog(ConfigElectricArchiveCatalogSaveDto saveDto) {
        String archiveId = saveDto.getArchiveId();
        ConfigElectricArchiveMain configElectricArchiveMain = configElectricArchiveMainMapper.selectByPrimaryKey(archiveId);
        if (ObjectUtils.isEmpty(configElectricArchiveMain)){
            throw  new BusinessException("电子卷宗不存在");
        }
        UserDTO userInfo = UserUtil.getUserInfo();
        //删除历史数据
        List<ConfigElectricArchiveCatalogDetail> electricArchiveCatalog = this.getElectricArchiveCatalog(archiveId);
        if (ObjectUtil.isNotEmpty(electricArchiveCatalog) && electricArchiveCatalog.size()>0){
            Example example =new Example(ConfigElectricArchiveCatalogDetail.class);
            example.createCriteria().andEqualTo("archiveId",archiveId);
            configElectricArchiveCatalogDetailMapper.deleteByExample(example);
        }
        //保存目录信息
        List<ConfigElectricArchiveCatalogDetailSaveDto>  detailSaveDtoList = saveDto.getArchiveCatalogDetailList();
        ConfigElectricArchiveCatalogDetail catalogDetail=new ConfigElectricArchiveCatalogDetail();
        catalogDetail.setArchiveId(archiveId);
        catalogDetail.setCreateTime(LocalDateTime.now());
        catalogDetail.setCreateUserId(userInfo.getUserId()+"");
        catalogDetail.setCreateUserName(userInfo.getRealName());
        for (ConfigElectricArchiveCatalogDetailSaveDto detailDto:detailSaveDtoList){
            CustomCheckParamUtil.doCheck(detailDto);
            catalogDetail.setId(BaseUtil.createUid());
            catalogDetail.setDocumentEvidenceCode(detailDto.getDocumentEvidenceCode());
            catalogDetail.setDocumentEvidenceName(detailDto.getDocumentEvidenceName());
            catalogDetail.setDocumentEvidenceSource("tycf");
            catalogDetail.setDocumentEvidenceType("S001");
            catalogDetail.setSort(detailDto.getSort());

            configElectricArchiveCatalogDetailMapper.insertSelective(catalogDetail);
        }
        return 1;
    }
}
